<template>
  <div class="page-common-layout custom-tablePage-container">
    <div class="screen-conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <a-input
            placeholder="请输入通知单编号"
            style="width:200px"
            v-model="condition.notificationNumber"
          ></a-input>
          <!--  -->
          <a-input
            placeholder="请输入变更名称"
            style="width:200px"
            v-model="condition.name"
          ></a-input>
          <!--  -->
          <a-input
            placeholder="请输入变更原因"
            style="width:200px"
            v-model="condition.reason"
          ></a-input>
          <!--  -->
          <a-select
            v-model="condition.major"
            placeholder="请选择专业"
            showSearch
            allowClear
            :filter-option="filterOption"
          >
            <a-select-option
              :value="item.dicItemCode"
              v-for="item in majorSource"
              :key="item.dicItemCode"
            >
              {{ item.dicItemName }}
            </a-select-option>
          </a-select>
          <!--  -->
          <a-input
            style="width:200px"
            v-model="condition.creator"
            placeholder="请输入提交人"
          ></a-input>
          <!--  -->
          <a-input
            style="width:200px"
            v-model="condition.position"
            placeholder="请输入变更位置"
          ></a-input>
          <!-- 变更金额 -->
          <div class="input-group-warp">
            <a-input
              addon-after="万元"
              placeholder="变更金额"
              style="width:183px;"
              class="addon-input-warp"
              v-model="condition.startNum"
            />
            <a-input
              addon-after="万元"
              placeholder="变更金额"
              style="width:183px;"
              class="addon-input-warp"
              v-model="condition.endNum"
            />
          </div>
          <!--  -->
          <a-select
            v-model="condition.commonChangeType"
            placeholder="请选择变更分类"
            showSearch
            allowClear
            :filter-option="filterOption"
          >
            <a-select-option
              :value="item.dicItemCode"
              v-for="item in changeTypeSource"
              :key="item.dicItemCode"
            >
              {{ item.dicItemName }}
            </a-select-option>
          </a-select>

          <!--  -->
          <a-range-picker
            :placeholder="['变更开始时间', '变更结束时间']"
            format="YYYY-MM-DD"
            valueFormat="YYYY-MM-DD"
            v-model="condition.date"
            :show-time="false"
          />
          <!--  -->
          <a-range-picker
            :placeholder="['提交开始日期', '提交结束日期']"
            format="YYYY-MM-DD"
            valueFormat="YYYY-MM-DD"
            v-model="condition.createTime"
            :show-time="false"
          />
        </template>
        <template slot="operate">
          <a-button class="cu-btn-primary" icon="search" @click="search()">
            查询
          </a-button>
          <a-button class="cu-btn-filter" icon="reload" @click="reset()">
            重置
          </a-button>
        </template>
      </ScreenConditions>
    </div>
    <div class="table-body-container">
      <div class="table-operate">
        <a-button
          @click="handleWrapPage({}, 1)"
          class="cu-btn-primary margin-bottom-xs"
          icon="plus"
          v-action="'sjbg_table_add'"
        >
          新增
        </a-button>
      </div>
      <div class="table-main">
        <!-- -->
        <vxe-table
          ref="xTable"
          align="left"
          show-overflow
          show-header-overflow
          border="inner"
          height="99%"
          :data="tableData"
          :column-config="{ resizable: true }"
          size="small"
          :loading="loading"
          :row-config="{ keyField: 'uuid' }"
          :expand-config="{
            reserve: true,
            visibleMethod: visibleMethod,
          }"
        >
          <vxe-column type="expand" width="26">
            <template #content="{ row }">
              <div class="expand-wrapper">
                <vxe-table
                  align="center"
                  border="inner"
                  :data="row.historys || []"
                  :column-config="{ resizable: true }"
                  show-overflow
                  show-header-overflow
                  size="small"
                >
                  <vxe-column field="versionNumber" title="版本号">
                  </vxe-column>
                  <vxe-column field="majorDesc" title="专业"> </vxe-column>
                  <vxe-column field="name" title="变更名称" align="left">
                  </vxe-column>
                  <vxe-column
                    field="reason"
                    title="变更原因"
                    align="left"
                    width="200"
                  >
                  </vxe-column>
                  <vxe-column field="commonChangeTypeDesc" title="变更分类">
                  </vxe-column>
                  <vxe-column field="position" title="变更位置"> </vxe-column>
                  <vxe-column field="amountMoney" title="变更金额">
                    <template #default="{ row }">
                      {{ row.amountMoney || "-" }} 万元
                    </template>
                  </vxe-column>
                  <vxe-column field="notificationNumber" title="变更通知单编号">
                  </vxe-column>
                  <vxe-column field="time" title="变更时间"> </vxe-column>
                  <vxe-column field="reason" title="驳回原因"> </vxe-column>
                  <vxe-column
                    field="operate"
                    width="60"
                    title="操作"
                    fixed="right"
                  >
                    <template #default="{ row }">
                      <div class="text-button-group">
                        <a-tooltip placement="top">
                          <template slot="title">
                            查看
                          </template>
                          <a-icon
                            type="file-search"
                            @click="handleDetailPage(row)"
                          />
                        </a-tooltip>
                      </div>
                    </template>
                  </vxe-column>
                </vxe-table>
              </div>
            </template>
          </vxe-column>
          <vxe-column
            type="seq"
            width="50"
            title="序号"
            align="center"
          ></vxe-column>
          <vxe-column field="majorDesc" title="专业"> </vxe-column>
          <vxe-column field="notificationNumber" title="变更通知单编号">
          </vxe-column>
          <vxe-column field="commonChangeTypeDesc" title="变更分类">
          </vxe-column>
          <vxe-column field="name" title="变更名称" align="left"> </vxe-column>
          <vxe-column field="reason" title="变更原因" align="left" width="200">
          </vxe-column>
          <vxe-column field="position" title="变更位置"> </vxe-column>
          <vxe-column field="amountMoney" title="变更金额">
            <template #default="{ row }">
              {{ row.amountMoney || "-" }} 万元
            </template>
          </vxe-column>
          <vxe-column field="time" title="变更时间"> </vxe-column>
          <vxe-column title="状态" width="100">
            <template #default="{ row }">
              <div class="state" :class="[filterStateClass(row.approveStatus)]">
                {{ filterStateName(row.approveStatus) }}
              </div>
            </template>
          </vxe-column>
          <vxe-column
            field="operate"
            width="180"
            title="操作"
            fixed="right"
            align="left"
          >
            <template #default="{ row }">
              <div class="text-button-group">
                <!-- 详情 -->
                <a-tooltip placement="top">
                  <template slot="title">
                    查看
                  </template>
                  <a-icon type="file-search" @click="handleDetailPage(row)" />
                </a-tooltip>
                <!-- 上传变更批复文件 -->
                <a-tooltip
                  placement="top"
                  v-action="'sjbg_table_upload'"
                  v-if="row.uploadCad"
                >
                  <template slot="title">
                    上传变更批复文件
                  </template>
                  <icon-fonts
                    icon="icon-tubiao_tuzhiguanli"
                    class="drawing"
                    @change="handleUpload(row)"
                  ></icon-fonts>
                </a-tooltip>
                <!-- 审批记录 -->
                <a-tooltip placement="top" v-if="row.approveRecord">
                  <template slot="title">
                    审批记录
                  </template>
                  <icon-fonts
                    icon="icon-shenpi"
                    class="record"
                    @change="gotoApprovalRecord(row)"
                  ></icon-fonts>
                </a-tooltip>
                <!-- 提交 -->
                <a-tooltip
                  placement="top"
                  v-if="row.submit"
                  v-action="'sjbg_table_submit'"
                >
                  <template slot="title">
                    提交
                  </template>
                  <icon-fonts
                    icon="icon-fabu-tijiao-tijiaobanben"
                    class="submit"
                    @change="handleSubmit(row)"
                  ></icon-fonts>
                </a-tooltip>
                <!-- 审批 -->
                <a-tooltip placement="top" v-if="row.approve">
                  <template slot="title">
                    审批
                  </template>
                  <icon-fonts
                    icon="icon-shenpi1"
                    class="approve"
                    @change="handleWrapPage(row, 4)"
                  ></icon-fonts>
                </a-tooltip>
                <!-- 编辑 -->
                <a-tooltip v-action="'sjbg_table_edit'" v-if="row.edit">
                  <template slot="title">
                    编辑
                  </template>
                  <a-icon type="edit" @click="handleWrapPage(row, 2)" />
                </a-tooltip>
                <!-- 删除 -->
                <a-tooltip v-action="'sjbg_table_del'" v-if="row.del">
                  <template slot="title">
                    删除
                  </template>
                  <a-icon type="delete" @click="handleDelClick(row)" />
                </a-tooltip>
              </div>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="table-pager">
        <vxe-pager
          size="small"
          background
          :current-page.sync="pager.page"
          :page-size.sync="pager.pageSize"
          :total="pager.total"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes']"
          :page-sizes="[20, 50, 100, 500]"
          @page-change="paginationChange"
        >
          <template #left>
            <div class="vxe-pager-left-tip-container">
              共<span>{{ pager.total }}</span
              >条
            </div>
          </template>
        </vxe-pager>
      </div>
    </div>
    <!-- 新增/编辑/查看 -->
    <wrapPage
      :key="wrapPage.key"
      :visible.sync="wrapPage.visible"
      :parameter="wrapPage.row"
      :pageOperate="wrapPage.operate"
    ></wrapPage>

    <!-- 审批记录 -->
    <comment-modal ref="commentModal"></comment-modal>

    <!-- 上传文件 -->
    <uploadFileModal
      :visible.sync="uploadFileModal.visible"
      :row="uploadFileModal.row"
    ></uploadFileModal>

    <!-- 上传压缩包 -->
    <uploadZipModal
      :visible.sync="uploadZipModal.visible"
      :row="uploadZipModal.row"
      @onRefresh="search()"
    ></uploadZipModal>

    <!-- 详情 -->
    <detailPage
      :key="detailPage.key"
      :visible.sync="detailPage.visible"
      :parameter="detailPage.row"
    ></detailPage>
  </div>
</template>

<script>
// 设计变更
import { _getUUID } from "@/utils/util.js";
import { downLoadBatchFileByArraybuffer } from "@/utils/TopVUI";
import ScreenConditions from "@/components/extendCustom/ScreenConditions";
import wrapPage from "./wrapPage.vue";
//审批记录
import commentModal from "@/pages/workflow/oaLeave/comment.vue";
//导入压缩包
import uploadZipModal from "./components/uploadZipModal.vue";
//上传文件
import uploadFileModal from "./components/uploadFileModal.vue";
//详情
import detailPage from "./detailPage.vue";
import { getDicItemByCode } from "@/api/common.js";
import {
  pageDesignChange,
  deleteDesignChange,
  submitDesignChange,
} from "@/api/design";
export default {
  components: {
    ScreenConditions,
    wrapPage,
    commentModal,
    uploadZipModal,
    detailPage,
    uploadFileModal,
  },
  data() {
    return {
      //专业
      majorSource: [],
      //变更分类
      changeTypeSource: [],
      //查询条件
      condition: {
        //变更通知单编号
        notificationNumber: "",
        //专业
        major: undefined,
        //变更分类
        commonChangeType: undefined,
        //变更名称
        name: "",
        //变更原因
        reason: "",
        //变更位置
        position: "",
        //变更金额
        startNum: "",
        endNum: "",
        //变更时间
        date: [],
        //提交时间
        createTime: [],
        //提交人
        creator: "",
      },
      //列表
      loading: false,
      tableData: [],
      pager: {
        page: 1,
        pageSize: 50,
        total: 0,
      },
      // 新增/编辑/查看
      wrapPage: {
        visible: false,
        key: _getUUID(),
        row: {},
        operate: 1,
      },
      //详情
      detailPage: {
        visible: false,
        key: _getUUID(),
        row: {},
        operate: 1,
      },
      //导入压缩包
      uploadZipModal: {
        visible: false,
        row: {},
      },
      //上传文件
      uploadFileModal: {
        visible: false,
        row: {},
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      this.majorSource = await this.getDicCodeData("major");
      this.changeTypeSource = await this.getDicCodeData("commonChangeType");
      await this.getTableData();
    },
    //查询字典表
    getDicCodeData(dicCode = "") {
      return new Promise((resolve, reject) => {
        getDicItemByCode({ code: dicCode })
          .then((res) => {
            let { data } = res;
            resolve(data);
          })
          .catch((_) => {
            reject();
          })
          .finally((_) => {});
      });
    },

    //上传
    handleUpload(row) {
      this.uploadFileModal.visible = true;
      this.uploadFileModal.row = row;
    },

    // 新增/编辑
    handleWrapPage(row, operate) {
      this.wrapPage.operate = operate;
      this.wrapPage.row = row;
      this.wrapPage.key = _getUUID();
      this.wrapPage.visible = true;
    },

    //详情
    handleDetailPage(row) {
      this.detailPage.row = row;
      this.detailPage.key = _getUUID();
      this.detailPage.visible = true;
    },

    //审批记录
    gotoApprovalRecord(record) {
      this.$refs.commentModal.getCommentList(record.uuid);
    },

    //删除
    handleDelClick(row) {
      let dataLength = this.tableData.length;
      let that = this;
      that.$confirm({
        title: "提示",
        content: "确定删除该数据吗？删除之后不可恢复！",
        okText: "确定",
        cancelText: "取消",
        icon: () =>
          that.$createElement("a-icon", {
            props: {
              key: "warning",
              type: "exclamation-circle",
              twoToneColor: "#faad14",
            },
          }),
        onOk() {
          deleteDesignChange({ uuid: row.uuid })
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                that.$message.success("删除成功", 1.5);
                if (dataLength == 1 && that.pager.page > 1) {
                  that.pager.page -= 1;
                }
                that.getTableData();
              } else {
                that.$message.error(res.message);
              }
            })
            .catch((_) => {});
        },
      });
    },

    //提交
    handleSubmit(row) {
      let that = this;
      let { uuid } = row;
      const h = this.$createElement;
      this.$confirm({
        title: "您确定提交所选内容?",
        content: "",
        okText: "确定",
        okType: "primary",
        cancelText: "取消",
        centered: true,
        icon: () =>
          h("a-icon", {
            props: {
              type: "question-circle",
              theme: "filled",
            },
            style: "color:#FAAD14",
          }),
        onOk() {
          that.loading = true;
          submitDesignChange({ uuid })
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                that.$message.success("提交成功", 1.5);
                that.getTableData();
              } else {
                that.$message.error(res?.message ?? "提交失败");
              }
            })
            .catch((_) => {
              that.$message.error(res?.message ?? "提交失败");
            })
            .finally(() => {
              that.loading = false;
            });
        },
        onCancel() {},
      });
    },

    //查询
    search() {
      this.pager.page = 1;
      this.pager.pageSize = 50;
      this.getTableData();
    },

    //重置
    reset() {
      this.condition = {
        //变更通知单编号
        notificationNumber: "",
        //专业
        major: undefined,
        //变更分类
        commonChangeType: undefined,
        //变更名称
        name: "",
        //变更原因
        reason: "",
        //变更位置
        position: undefined,
        //变更金额
        startNum: "",
        endNum: "",
        //变更时间
        date: [],
        //提交时间
        createTime: [],
        //提交人
        creator: "",
      };
      this.search();
    },
    visibleMethod({ row }) {
      if (row.historys && row.historys.length != 0) {
        return true;
      } else {
        return false;
      }
    },
    //查询列表数据
    getTableData() {
      this.loading = true;
      let { page, pageSize } = this.pager;
      let {
        notificationNumber,
        major = "",
        name,
        reason,
        position = "",
        startNum,
        endNum,
        date,
        createTime,
        creator,
        commonChangeType = "",
      } = this.condition;
      return new Promise((resolve, reject) => {
        pageDesignChange({
          page,
          pageSize,
          notificationNumber,
          major,
          name,
          reason,
          position,
          startNum,
          endNum,
          startTime: date?.[0] ?? "",
          endTime: date?.[1] ?? "",
          startTimeSub: createTime?.[0] ?? "",
          endTimeSub: createTime?.[1] ?? "",
          creator,
          commonChangeType,
        })
          .then((res) => {
            this.tableData = res.data?.rows || [];
            this.pager.total = res.data?.totalCount || 0;
            resolve();
          })
          .catch((_) => {
            this.tableData = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.loading = false;
          });
      });
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },

    filterStateName(val) {
      switch (val) {
        case 0:
          return "待提交";
        case 1:
          return "审批中";
        case 2:
          return "审批通过";
        case 3:
          return "驳回";
        default:
          return "-";
      }
    },

    filterStateClass(val) {
      switch (val) {
        case 0:
          return "default";
        case 1:
          return "ing";
        case 2:
          return "lock";
        case 3:
          return "error";
        default:
          return "";
      }
    },

    //select 搜索过滤
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.screen-conditions-container {
  .more {
    cursor: pointer;
    font-size: 14px;
    color: #1890ff;
    margin-left: 10px;
    padding-top: 6px;
    .anticon {
      font-size: 12px;
    }
  }
  .input-group-warp {
    display: flex;
    align-items: center;
    position: relative;
    &::after {
      content: "";
      position: absolute;
      width: 16px;
      border-bottom: 1px solid #bfbfbf;
      left: 186px;
      top: 18px;
    }
    /deep/.addon-input-warp {
      margin-right: 16px;
      &:first-child {
        margin-right: 30px;
      }
      .ant-input {
        margin-right: 0;
        width: 132px !important;
        border-right: none;
      }
      .ant-input-group-addon {
        display: inline-block;
        height: 32px;
        width: 44px;
        background: transparent;
        color: #bfbfbf;
        line-height: 32px;
      }
    }
  }
}

.expand-wrapper {
  padding: 10px;
  box-sizing: border-box;
}
.state {
  &::before {
    position: relative;
    display: inline-block;
    content: "";
    width: 8px;
    height: 8px;
    background: #239b53;
    border-radius: 0px 0px 0px 0px;
    border-radius: 50%;
    margin-right: 10px;
  }
  &.default {
    &::before {
      background: #8c8c8c;
    }
  }
  &.ing {
    &::before {
      background: #f07f17;
    }
  }
  &.lock {
    &::before {
      background: #239b53;
    }
  }

  &.error {
    &::before {
      background: #ff3f4a;
    }
  }
}
</style>
